//
//  HTTPTool.m
//  GentlemanAnime
//
//  Created by dlios on 15-6-29.
//  Copyright (c) 2015年 lanou3g.com 蓝欧科技. All rights reserved.
//

#import "HTTPTool.h"
#import "AFNetworking.h"

@implementation HTTPTool

// 封装GET请求
+ (void)get:(NSString *)url
       body:(id)body
httpResultStyle:(HTTPStyle)style
    success:(void (^)(id result))success
    failure:(void (^)(NSError *error))failure {
    // 1.获取AFN网络请求的管理类
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    // 2.判断返回的responseObject的数据类型
    switch (style) {
            // NSData类型
        case HTTP:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
            // JSON类型
        case JSON:
            break;
            // XML类型
        case XML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
    }
    
    // 3.让管理类调用GET请求
    [manager GET:url parameters:body success:^(AFHTTPRequestOperation *operation, id responseObject) {
        // 如果成功返回数据
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // 如果失败返回error信息
        if (failure) {
            failure(error);
        }
    }];
}

// 封装GET请求
+ (void)get1:(NSString *)url
       body:(id)body
httpResultStyle:(HTTPStyle)style
    success:(void (^)(id result))success
    failure:(void (^)(NSError *error))failure {
    // 1.获取AFN网络请求的管理类
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    // 2.判断返回的responseObject的数据类型
    switch (style) {
            // NSData类型
        case HTTP:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
            // JSON类型
        case JSON:
            break;
            // XML类型
        case XML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
    }
    
    [manager.requestSerializer setValue:@"http://imgsmall.dmzj.com/" forHTTPHeaderField:@"Referer"];
    // 3.让管理类调用GET请求
    [manager GET:url parameters:body success:^(AFHTTPRequestOperation *operation, id responseObject) {
        // 如果成功返回数据
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // 如果失败返回error信息
        if (failure) {
            failure(error);
        }
    }];
}


// 封装POST请求
+ (void)post:(NSString *)url
        body:(id)body
httpResultStyle:(HTTPStyle)style
requestBodyStyle:(RequestBodyStyle)bodyStyle
     success:(void (^)(id result))success
     failure:(void (^)(NSError *error))failure {
    // 1.获取AFN网络请求的管理类
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    // 2.判断request的类型
    switch (bodyStyle) {
        case BodyHTTP:
            manager.requestSerializer = [AFHTTPRequestSerializer serializer];
            break;
        case BodyJOSN:
            manager.requestSerializer = [AFJSONRequestSerializer serializer];
            break;
    }

    // 3.判断返回的responseObject的数据类型
    switch (style) {
        case HTTP:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        case JSON:
            break;
            case XML:
        manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
    }
    // 4.让管理类调用GET请求
    [manager POST:url parameters:body success:^(AFHTTPRequestOperation *operation, id responseObject) {
        // 如果成功返回数据
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // 如果失败返回error信息
        if (failure) {
            failure(error);
        }
    }];
}



@end
